import processing.pdf.*;
Table t;
/* Based off of some great example code
 from Quark http://processing.org/discourse/yabb2/YaBB.pl?num=1251333472/9 */

HashMap myMap;
int rowCount, labelCount;
int count;
float percent;

void setup(){
  size(200, 200, PDF, "out.pdf");  
  size(620, 480);  
  //t = new Table("reclass.tsv");
  t = new Table("wf08-09.tsv");
  rowCount = t.getRowCount(  );
  println(rowCount+" Total lines");
  println("------------------------------------");
  myMap = new HashMap();
  crunchData(); 
  displayMapContents(myMap);
  println("------------------------------------");  
}

void displayMapContents(HashMap hm){
  Iterator i = hm.entrySet().iterator();  // Get an iterator
  while (i.hasNext()){
    Map.Entry me = (Map.Entry)i.next();
    Integer value = (Integer)me.getValue(); // Retrieve the value as Integer (obj)
    percent = ((float)value/(float)rowCount)*100 ;
    float f = (float)Math.round(percent);
    print(f+"% ");
    println(me.getKey()+"["+me.getValue()+"]");
  }
}

void crunchData(){
  for (int i = 1; i < rowCount; i++) { // Start at 1 to skip the col title
    String value = t.getString(i, 5);

    if(myMap.containsKey(value)){
      count = (Integer)myMap.get(value);
      myMap.put(value,++count);
    }
    else if(!myMap.containsKey(value)){
      myMap.put(value,1);
      // println("Found "+value+" on line "+count); //DEBUGGING 
    }     
  }  
}  

